package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import www.lagou.entity.Account;
import www.lagou.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Account数据操作类
 *
 * @author Shuxian Huang
 */
public class AccountDao {

    /**
     * 通过卡号找到对应的账户
     * @param card
     * @return
     */
    public Account findAccountByCard(String card) throws SQLException {

        //1.创建QueryRunner
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //2、编写sql
        String sql = "select * from account where card = ?";

        //3、执行查询
        return qr.query(sql, new BeanHandler<Account>(Account.class), card);
    }

    /**
     * 修改指定账户的余额
     * @param card
     * @param balance
     */
    public int modifyAccountBalance(Connection con, String card, double balance) throws SQLException {

        //1.创建QueryRunner
        QueryRunner qr = new QueryRunner();

        //2、编写sql
        String sql = "update account set balance = ? where card = ?";

        //3、执行查询
        return qr.update(con, sql, balance, card);
    }
}
